package cn.itsource.zcn.basic.controller;

import cn.itsource.zcn.utils.AjaxResult;
import cn.itsource.zcn.utils.FastdfsUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastdfs")
public class FileUploadController {

    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        try {
            String originalFilename = file.getOriginalFilename();
            // 获取文件后缀名
            String substring = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            String path = FastdfsUtil.upload(file.getBytes(), substring);
//            System.out.println(path);
            return AjaxResult.me().setResultObj(path);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
